package com.woniuxy.controller;


import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.TbDepartment;
import com.woniuxy.entitys.TbHospital;
import com.woniuxy.entitys.TbSection;
import com.woniuxy.entitys.TbUser;
import com.woniuxy.authentication.PermissionAuth;
import com.woniuxy.entitys.*;
import com.woniuxy.remote.RemoteHospitalService;
import com.woniuxy.services.SectionService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginUser;
import com.woniuxy.utils.LoginUser;
import com.woniuxy.utils.ResultVO;
import com.woniuxy.utils.UserUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@RestController
public class SectionController {
    @Autowired
    private SectionService sectionService;
    @Autowired
    private UserUtils userUtilsl;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JWTUtils jwtUtils;
    @PostMapping("/section/getSection")
    public ResultVO getSection(@RequestBody Map<String, Object> map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        String phone = (String) map.get("phone");
        String sname = (String) map.get("sname");
        Integer did = null;
        Integer hid = null;
        if (map.get("hid") != null && map.get("hid") != "") {
            hid = (Integer) map.get("hid");
        }
        if (map.get("did") != null && map.get("did") != "") {
            did = (Integer) map.get("did");
        }
        PageInfo<TbSection> section = sectionService.getSection(pageNum, pageSize, phone, sname, did, hid);
        return section != null ? ResultVO.success("查询成功", section) : ResultVO.failure("查询失败");
    }

    @PostMapping("/section/updatePassword/{id}")
    public ResultVO UpdatePassword(@PathVariable("id") Integer id) {
        int i = sectionService.updatePassword(id);
        return i > 0 ? ResultVO.success("重置成功") : ResultVO.failure("重置失败");
    }

    @PostMapping("/section/softdel/{id}")
    public ResultVO softdel(@PathVariable("id") Integer id) {
        int i = sectionService.softdel(id);
        return i > 0 ? ResultVO.success("删除成功") : ResultVO.failure("删除失败");
    }
    @PostMapping("/section/getSectionByHid")
    public ResultVO getSectionByHid(@RequestBody Map<String,Object> map){
        Integer pageNum=(Integer) map.get("pageNum");
        Integer pageSize=(Integer) map.get("pageSize");
        String phone=(String)map.get("phone");
        String sname=(String)map.get("sname");
        Integer did=0;
        Integer hid;
        hid = getHid();
        if (hid == null){
            hid = 0;
        }

        PageInfo<TbSection> section = sectionService.getSection(pageNum, pageSize, phone, sname, did, hid);
        return section!=null?ResultVO.success("查询成功",section):ResultVO.failure("查询失败");
    }

    private Integer getHid() {
        Integer hid;
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String header = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(header);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:" + username);
        TbUser user = loginUser.getUser();
        Integer uid = user.getUid();
        hid = sectionService.getHidByUid(uid);
        return hid;
    }

    @PostMapping("section/addHead")
    public ResultVO addHead(@RequestBody Map<String,String> map){
        String gender = map.get("gender");
        String phone = map.get("phone");
        String sname = map.get("sname");
        int i = sectionService.saveAdd(gender,phone,sname,getHid());

        return i>0?ResultVO.success(""):ResultVO.failure("");
    }
    @GetMapping("section/removeHead/{id}")
    public ResultVO removeHead(@PathVariable Integer id){
        int i = sectionService.removeHead(id);
        return i>0?ResultVO.success(""):ResultVO.failure("");
    }
    @GetMapping("section/getNullDep")
    public ResultVO getDep(){

        List<TbDepartment> list = sectionService.getNullDep(getHid());
        return list!=null?ResultVO.success("",list):ResultVO.failure("");
    }
    @PostMapping("section/assDepart")
    public ResultVO saveAss(@RequestBody Map<String,Integer> map){
        Integer sid = map.get("uid");
        Integer did = map.get("did");
        int i = sectionService.saveAss(sid,did);
        return i>0?ResultVO.success(""):ResultVO.failure("");
    }

    @PostMapping("/section/getAllBed")
    public ResultVO getAllBed(@RequestBody Map<String, Integer> map) {
        LoginUser loginUser = userUtilsl.getLoginUser();
        List<TbBed> allBed = sectionService.getAllBed(map.get("pageNum"), map.get("pageSize"), map.get("bedStatusValue"), loginUser.getUser().getUid());
        if (allBed.size() == 0) {
            return ResultVO.failure("没有数据");
        }
        PageInfo<TbMalfunction> pageInfo = new PageInfo(allBed);
        return ResultVO.success("查询成功", pageInfo);
    }

    @PostMapping("/section/getAllBedByFuzz")
    public ResultVO getAllBedByFuzz(@RequestBody Map<String, String> map) {
        LoginUser loginUser = userUtilsl.getLoginUser();
        List<TbBed> allBedByFuzz = sectionService.getAllBedByFuzz(Integer.parseInt(map.get("pageNum")), Integer.parseInt(map.get("pageSize")), map.get("searchValue"), loginUser.getUser().getUid());
        if (allBedByFuzz.size() == 0) {
            return ResultVO.failure("没有数据");
        }
        PageInfo<TbBed> pageInfo = new PageInfo(allBedByFuzz);
        return ResultVO.success("查询成功", pageInfo);
    }

    /**
     * 点击新增模态框后
     *
     * @return
     */
    @GetMapping("/section/showAdd")
    public ResultVO showAdd() {
        LoginUser loginUser = userUtilsl.getLoginUser();
        TbUser user = loginUser.getUser();
        TbSection tbSection = sectionService.getSectionInfoByUid(user.getUid());
        return ResultVO.success("查询成功", tbSection);
    }

    /**
     * 增加床位
     *
     * @return
     */
    @PostMapping("/section/addBed")
    public ResultVO addBed(@RequestBody TbBed tbBed) {
        if (sectionService.addBed(tbBed)) {
            return ResultVO.success("添加成功");
        } else {
            return ResultVO.failure("添加失败");
        }
    }

    /**
     * 修改床位
     *
     * @return
     */
    @PostMapping("/section/updateBed")
    public ResultVO updateBed(@RequestBody TbBed tbBed) {
        if (sectionService.updateBed(tbBed)) {
            return ResultVO.success("修改成功");
        } else {
            return ResultVO.failure("修改失败");
        }
    }

    /**
     * 批量删除床位
     *
     * @return
     */
    @PostMapping("/section/delSomeBed")
    public ResultVO delSomeBed(@RequestBody Map<String, Object> map) {
        if (sectionService.delSomeBed((List) map.get("bids"))) {
            return ResultVO.success("删除成功");
        } else {
            return ResultVO.failure("删除失败");
        }
    }
    @GetMapping("section/getMyself")
    public ResultVO getMyself(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        TbSection myself= sectionService.getMyself(token);
        return  myself!=null?ResultVO.success("查询成功",myself):ResultVO.failure("查询失败");
    }
    @PostMapping("section/updateMyself")
    public ResultVO updateMyself(@RequestBody TbSection tbSection){
        int i =sectionService.updateMyself(tbSection);
        return i>0?ResultVO.success("修改成功"):ResultVO.failure("修改失败");
    }

}
